﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.DataModel;
using Chinesys.Entities;

namespace Chinesys.Domain.DataMapper
{
    public static class ProductDataMapper
    {
        public static Product ProductFromEntityToRow(ProductEntity entity)
        {
            Product row;

            if (entity != null)
            {
                row = new Product();
                row.CategoryId = entity.CategoryId;
                row.Description = entity.Description;
                row.Id = entity.Id;
                row.ImageSrc = entity.ImageSrc;
                row.Name = entity.Name;
                row.Price = entity.Price;
                row.Rating = entity.Rating;
                row.SubCategoryId = entity.SubCategoryId;
                row.TotalReviews = entity.TotalReviews;
                row.Active = entity.Active;
            }
            else
                row = null;

            return row;
        }

        public static ProductEntity ProductFromRowToEntity(Product row)
        {
            ProductEntity entity;

            if (row != null)
            {
                entity = new ProductEntity();

                entity.CategoryId = row.CategoryId;
                entity.Description = row.Description;
                entity.Id = row.Id;
                entity.ImageSrc = row.ImageSrc;
                entity.Name = row.Name;
                entity.Price = row.Price;
                entity.Rating = row.Rating;
                entity.SubCategoryId = row.SubCategoryId;
                entity.TotalReviews = row.TotalReviews;
                entity.CurrentState = RowState.Original;
                entity.Active = row.Active;
                entity.Category = row.Category == null ? null : CategoriesDataMapper.CategoriesFromRowToEntity(row.Category);
                entity.SubCategory = row.SubCategory == null ? null : SubCategoryDataMapper.SubCategoryFromRowToEntity(row.SubCategory);
            }
            else
                entity = null;

            return entity;
        }
    }
}
